function [ output_args ] = loadTransformedData( inputDataFolder, backgroundImage, videoFolder, outputFolder )
%the function 
global meanVals;
meanVals = [];
lstAll = dir(inputDataFolder);
lstFileName = {};

%Method to be used for briefcase detection
method =2;

%get a list of all the filenames from the input data folder
for i = 1:length(lstAll)
    %filter out the directory and the files we do not need
    if (lstAll(i).isdir == 0 && ~strcmp(lstAll(i).name,'.directory'))
       lstFileName = [lstFileName; lstAll(i).name];
    end
end
%get the number of inpu files we have aquired
numFile = length(lstFileName);

%get the first frame of the set
%we use the first frame to synchronize and extract the back plane for the
%projection
firstFameName = strcat(inputDataFolder, lstFileName(1));
firstFrame = load(firstFameName{1});
xyz1 = firstFrame.xyzImage;
rgb1 = firstFrame.rgbImage;

%define the number of samples for points of the plane
sampleNo = 100;

%generate the point coefficients randomly for finding the plane
cols = uint16(184 + (427-184).*rand(sampleNo,1));
rows = uint16(41 + (457-41).*rand(sampleNo,1));

points = ones(sampleNo,3);

%store the xyz points
for i=1:sampleNo
  points(i,:)= xyz1(rows(i),cols(i),:);
end

%get the plane coeficients that best fits the points
coeff = fitplane(points);

%initialize a matrix of euclidean distances to the plane
dist = ones(480,640);
%make the default values -1
dist = dist.*-1;

UV=zeros(4,2);
XY=zeros(4,2);
UV=[[41,184]',[41,427]',[473,453]',[472,158]']';    % target points
XY=[[1,1]',[1,338]',[450,338]',[450,1]']';    % source points

P=esthomog(UV,XY,4);    % estimate homography mapping UV to XY

% get input background image and its size
inimage=imread(backgroundImage,'jpg');
[IR,IC,D]=size(inimage);
v=zeros(3,1);


%Write the files to create an AVI Video
vw = VideoWriter('AV_movie.avi');
vw.FrameRate = 6;
vw.open();

videoFrameCounter = 0;
for idx=1:36
    fullFileName = strcat(inputDataFolder, lstFileName(idx));
    disp(fullFileName);
    %load transformed data
    xyzrgbStruct = load(fullFileName{1});
    
    %load from the transformed 
    rgbImage = xyzrgbStruct.rgbImage;
    xyzImage = xyzrgbStruct.xyzImage;
    
    rgbBrief = false(480,640);
    
    %at each loop 
    for i=1:480
        for j=1:640
            point = xyzImage(i,j,:);
            point = permute(point,[3 1 2]);
            dist(i,j) = getEuclideanDistance(point,coeff);
            
            v=P*[i,j,1]';        % project destination pixel into source
            y=round(v(1)/v(3));  % undo projective scaling and round to nearest integer
            x=round(v(2)/v(3));
            
            
            if dist(i,j)<0.05 && dist(i,j)~=-1 && (x >= 1) && (x <= IC) && (y >= 1) && (y <= IR)
                %overlay the color from the background image
                rgbImage(i,j,:) = inimage(y,x,:);
            end
            
            if dist(i,j)>0.75 && dist(i,j)< 2.2 && ... 
                    rgbImage(i,j,1) < 40 && rgbImage(i,j,2) < 40 && rgbImage(i,j,3) < 40
                rgbBrief(i,j) = 1;
            end
        end
    end
    
    %If the frame contains the briefcase
    if idx > 13 && idx < 29
       
        videoFrameCounter = videoFrameCounter+1;
        frameName = strcat(videoFolder,'flip_',num2str(videoFrameCounter,'%d'),'.jpg');
        videoFrame = imread(frameName);
       if method == 1
       %Method 1
       %**********************************************
        rgbImage = fillBriefcase(rgbBrief, rgbImage, xyzImage, videoFrame);
       %Method 2
       %**********************************************
       elseif method ==2
        
        %Process the range values to find the image
        imageZ = xyzImage(:,:,3);

        %Find the closest point in the image, rows, cols
        [row, col] = find(imageZ == max(imageZ(imageZ<0)));

        %Find the closest point in the image
        closestPoint = imageZ(row(1),col(1));
        
        %Find the briefcase corner points and the plane
        [briefcaseImage] = findBriefcase(rgbImage, xyzImage, closestPoint, videoFrame,0);
        rgbImage = briefcaseImage;
        %*******************************************
       end
    end
    %Remove the black background noise
    %*****************************************************************
    %Find all points that are black along all three colour channels
    [rowsGrow, colsGrow] = find(rgbImage(:,:,1)==0 & rgbImage(:,:,2)==0 & rgbImage(:,:,3)==0);
    %Filter out unnecessary points
    [filteredRows, filteredCols] = find((rowsGrow>39 & rowsGrow<470) & (colsGrow>17 & colsGrow <610));

    %Average out the points using a nxn neighborhood
    n = 2;
    for ii=1:size(filteredRows,1)
    R = 0;
    G = 0;
    B = 0;
    currentRow = rowsGrow(filteredRows(ii));
    currentCol = colsGrow(filteredRows(ii));

    for jj = currentRow -n:currentRow + n
    %rgbImage(jj, currentCol -n: currentCol + n ,1);
    R = R + sum(rgbImage(jj, currentCol -n: currentCol + n ,1));
    G = G + sum(rgbImage(jj, currentCol -n: currentCol + n ,2));  
    B = B + sum(rgbImage(jj, currentCol -n: currentCol + n ,3));
    end
    rgbImage(currentRow,currentCol,1) = R/(2*n +1)^2;
    rgbImage(currentRow,currentCol,2) = G/(2*n +1)^2;  
    rgbImage(currentRow,currentCol,3) = B/(2*n +1)^2;

    end
    %*********************************************

    %display the rendered image
    imshow(rgbImage);
    outFile = strcat(outputFolder,'frame_',num2str(idx,'%0.2d'),'.jpg');
    imwrite(rgbImage,outFile,'jpg');
    %Create the video
    image = rgbImage; 
    imshow(uint8(image));
    writeVideo(vw,getframe(gcf));
    pause(0.3);
end
%Close the video player
close(vw); 

end

